import 'package:flutter/material.dart';

import 'map-marker.dart';
typedef MarkerTapDownCallback = void Function(MapMarker,TapDownDetails);
typedef MarkerTapUpCallback = void Function(MapMarker);
typedef MarkerTapCancelCallback = void Function(MapMarker);
typedef MarkerDragStarted = void Function(MapMarker);
typedef MarkerDragEnd = void Function(DraggableDetails,MapMarker);
class MapMarkerWidget extends StatelessWidget {
  MapMarker marker; //标注集合
  bool enableDrag; //是否禁止标注被拖拽
  MarkerTapDownCallback markerTapDownCallback;
  MarkerTapUpCallback markerTapUpCallback;
  MarkerTapCancelCallback markerTapCancelCallback;
  MarkerDragStarted markerDragStarted;
  MarkerDragEnd markerDragEnd;

  MapMarkerWidget({super.key,
    required this.marker,
    required this.enableDrag,
    required this.markerTapDownCallback,
    required this.markerTapUpCallback,
    required this.markerTapCancelCallback,
    required this.markerDragStarted,
    required this.markerDragEnd
  });



  Widget buildMapMarker (bool isfeedback){
    return ColorFiltered(
      colorFilter: ColorFilter.mode(
        Colors.black.withOpacity(isfeedback ? 0.2 : 0), // 半透明黑色遮罩
        BlendMode.srcATop,
      ),
      child: Container(
        width: 40,
        height: 50,
        alignment: Alignment.topCenter,
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage('images/marker-icon.png'),
            fit: BoxFit.cover,
          ),
        ),
        child:Container(
          // 背景图片两边阴影不一致临时解决
          margin: const EdgeInsets.only(top: 5,right: 2),
          child:ClipOval(
            child: Image.network(
              'https://fastly.picsum.photos/id/1043/200/300.jpg?hmac=3l-zIM3sjEYfLwln1cOurui-983Bxq1zjEYt9HeScyQ',
              width: 28,
              height: 28,
              fit: BoxFit.cover,
            ),
          ),
        )
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
      left: marker.position.dx,
      top: marker.position.dy,
      child:enableDrag ? LongPressDraggable(
        //确保拖拽行为从按下事件开始，而不是从长按事件开始。这样可以避免在长按过程中触发 GestureDetector 的 onTapDown 事件。
        feedbackOffset: Offset.zero,
        //在拖拽过程中显示的反馈组件，这里是一个红色的标注图标。
        feedback: buildMapMarker(true),
        //拖拽过程中显示的替代组件，这里是一个空的 Container。
        childWhenDragging: Container(),
        //实际的标注组件，这里是一个红色的标注图标。
        child: GestureDetector(
          onTapDown: (details)=> markerTapDownCallback(marker,details),
          // 处理标注的点击按下事件
          onTap: () => markerTapUpCallback(marker),
          onTapCancel: () => markerTapCancelCallback(marker),
          child:buildMapMarker(false),
        ),
        onDragStarted:()=>markerDragStarted(marker),
        // 处理标注的拖拽结束事件
        onDragEnd: (details)=>markerDragEnd(details,marker)
      )
    : buildMapMarker(false));
  }
}